﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RedSand.Site.Models;
using RedSand.Web.Mvc;

namespace RedSand.Site.Controllers
{
    public class RequestController : BaseController
    {

        public ActionResult Manager()
        {
            return View(new ManagerModel { Title = Translate("RequestManager", "Request") });
        }
        public ActionResult List(RequestSearchModel searchModel, int? page)
        {
            int Total = 0;
            List<RequestInfo> list = RsDataManager.GetList<RequestInfo>("RequestSearch", searchModel, page ?? 1, 10, ref Total);
            ViewBag.RouteValues = searchModel;
            ViewBag.TotalRecord = Total;
            return PartialView(list);
        }
        [HttpGet, AjaxOrChildAction]
        public ActionResult Create()
        {
            return PartialView();
        }

        [HttpPost, AjaxOrChildAction]
        public ActionResult Create(RequestInfo info)
        {
            if (ModelState.IsValid)
            {
                info.Language = LanguageCode;
                if (RsDataManager.Save("RequestSave", info))
                {
                    AddMessage(Message.CreateSuccess);
                    return RedirectToAction("List");
                }
                return AjaxMessage(Message.CreateError);
            }
            return AjaxModelError();
        }


        [HttpGet, AjaxOrChildAction]
        public ActionResult Edit(int id)
        {
            return PartialView(RsDataManager.GetInfo<CategoryInfo>("RequestGetInfo", "RequestID", id));
        }

        [HttpPost, AjaxOrChildAction]
        public ActionResult Edit(RequestInfo info)
        {
            if (ModelState.IsValid)
            {
                if (RsDataManager.Save("RequestSave", info))
                {
                    AddMessage(Message.UpdateSuccess);
                    return RedirectToAction("List");
                }
                return AjaxMessage(Message.UpdateError);
            }
            return AjaxModelError();
        }

        [AjaxOrChildAction]
        public ActionResult Delete(int[] id)
        {
            if (id != null)
            {
                int count = id.Count();
                foreach (int i in id)
                {
                    if (RsDataManager.Delete("RequestDelete", "RequestID", i))
                    {
                        count--;
                    }
                }
                if (count > 0)
                {
                    AddMessage(string.Format(Translate("CannotDeleteAnyRecords", "Message"), count));
                }
                else
                {
                    AddMessage(Message.DeleteSuccess);
                }
            }
            return RedirectToAction("List");
        }


        [AjaxOrChildAction]
        public ActionResult GetListRequest(int StartId)
        {
            List<RequestInfoFull> lst = RsDataManager.GetList<RequestInfoFull>("RequestGetListByStartId", new { StartId = StartId, TotalRecord = 5, language = LanguageCode });
            return PartialView(lst);
        }
        [HttpGet]
        public ActionResult TopRequest()
        {
            List<RequestInfoFull> lst = RsDataManager.GetList<RequestInfoFull>("RequestGetListByStartId", new { StartId = 0, TotalRecord = 8, language = LanguageCode });
            return PartialView(lst);
        }

        [HttpGet, AjaxOrChildAction]
        public ActionResult PostRequest()
        {
           
            return PartialView("PostRequest");
        }
        [HttpPost, AjaxOrChildAction]
        public ActionResult PostRequest(RequestInfo info)
        {
            info.CreateDate = DateTime.Now;
            info.Language = RedSand.Web.Language.RsLanguage.LanguageCode;
            info.UserID = UserID;
            info.Status = 0;
            if (RsDataManager.Save("RequestSave", info))
                return Json("Ok");
            else
                return Json("Ok");
        }
    }
}
